const Controller = require('../../controllers/controllers').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'新消息提醒',
    columnUpdate: -1, //关注栏目更新，1是,-1否
    aerobicUpdates: -1, //提醒有氧更新，1是,-1否
    FollowersUpdate: -1, //	关注人更新，1是 ,-1否 
    specificUpdates: -1, //接收具体更新内容，1是,-1否
    tmplIds: ['bwtM6megTetC9O34-USknNYsBD_EFjBwuNI4wCln0KY']
  },
  timer: null,
  // 防抖封装方法
  debounce(fun) {
    if (this.timer) {
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(() => {
      fun.call(this)
    }, 400)
  },
  // 提醒有氧更新
  aerobicUpdates(e) {
    let that = this
    wx.requestSubscribeMessage({
      tmplIds: that.data.tmplIds,
      success(res) {
        wx.showLoading({
          title: '加载中~~~'
        })
        console.log('requestSubscribeMessage--res接口调用成功', res)
        let flag = res['bwtM6megTetC9O34-USknNYsBD_EFjBwuNI4wCln0KY'] //判断是点击取消还是授权
        console.log('flag', flag)
        if (flag == 'reject') {
          console.log('取消')
          that.setData({
            aerobicUpdates:-1,
            specificUpdates: -1
          })

        } else {
          console.log('授权')
          that.setData({
            aerobicUpdates:1
          })
        }
        that.updateNotifySetting()
        wx.showToast({
          title: '授权成功',
          duration: 1000
        })
      },
      fail(err) {
        console.log('requestSubscribeMessage--err接口调用失败', err)
      }
    })

  },
  // 关注栏目更新
  columnUpdate(e) {
    let that = this
    console.log(e)
    that.debounce(() => {
      let columnUpdate = this.data.columnUpdate == -1 ? 1 : -1
      if (columnUpdate == -1) {
        this.setData({
          specificUpdates: -1
        })
      }
      that.setData({
        columnUpdate
      })
      that.updateNotifySetting('', columnUpdate, '', '')

    }) 
  },
  // 关注人更新
  FollowersUpdate(e) {
    let that = this
    console.log(e)
    that.debounce(() => {
      let FollowersUpdate = this.data.FollowersUpdate == -1 ? 1 : -1
      if (FollowersUpdate == -1) {
        this.setData({
          specificUpdates: -1
        })
      }
      that.setData({
        FollowersUpdate
      })
      that.updateNotifySetting(FollowersUpdate, '', '', '')
    })
  },

  // 接收具体更新内容
  // 调起客户端小程序订阅消息界面，返回用户订阅消息的操作结果
  /* 
  1、微信开发者基础库：v2.9.0以上；
  2、激发条件：bindtap;
  3、记录用户是否接受：
  */
  // 授权步骤最好手机测试，开发者工具瞎几把乱来的，不可靠
  specificUpdates(e) {
    let that = this
    wx.requestSubscribeMessage({
      tmplIds: that.data.tmplIds,
      success(res) {
        wx.showLoading({
          title: '加载中~~~'
        })
        console.log('requestSubscribeMessage--res接口调用成功', res)
        let flag = res['bwtM6megTetC9O34-USknNYsBD_EFjBwuNI4wCln0KY'] //判断是点击取消还是授权
        // let specificUpdates = that.data.specificUpdates == -1 ? 1 : -1
        console.log('flag', flag)
        if (flag == 'reject') {
          console.log('取消')
          that.setData({
            aerobicUpdates: -1,
            columnUpdate: -1,
            FollowersUpdate: -1,
            specificUpdates: -1
          })

        } else {
          console.log('授权')
          that.setData({
            aerobicUpdates: 1,
            columnUpdate: 1,
            FollowersUpdate: 1,
            specificUpdates: 1
          })
        }
        that.updateNotifySetting()
        wx.showToast({
          title: '授权成功',
          duration: 1000
        })
      },
      fail(err) {
        console.log('requestSubscribeMessage--err接口调用失败', err)
      }
    })

  },
  // 更新接收具体内容
  updateNotifySetting(FollowersUpdate, columnUpdate, aerobicUpdates, specificUpdates) {
    // console.log(wx.getStorageSync('logintoken'))
    console.log('更新接收具体内容')
    if (this.data.FollowersUpdate == 1 && this.data.columnUpdate == 1 && this.data.aerobicUpdates == 1) {
      specificUpdates = 1
    }
    let params = {
      followUserUpdate: FollowersUpdate || this.data.FollowersUpdate,
      followCategoryUpdate: columnUpdate || this.data.columnUpdate,
      youyangUpdate: aerobicUpdates || this.data.aerobicUpdates,
      detailUpdate: specificUpdates || this.data.specificUpdates
    }
    Controller.updateNotifySetting(params)
      .then(res => {
        console.log('修改数据', res)
        if (res.code == 0) {
          wx.showToast({
            title: '修改成功',
            duration: 1000
          })
          this.onLoad()
        }
      }).catch(err => {
        console.log('err', err)
      })
    wx.hideLoading()
  },
  // 获取数据信息
  notifySetting() {
    Controller.notifySetting()
      .then(res => {
        console.log('获取数据信息', res)
        if (res.code == 0 && res.data.baseData) {
          this.setData({
            FollowersUpdate: res.data.baseData.followUserUpdate,
            columnUpdate: res.data.baseData.followCategoryUpdate,
            aerobicUpdates: res.data.baseData.youyangUpdate,
            specificUpdates: res.data.baseData.detailUpdate
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.notifySetting()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·新消息提醒',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·新消息提醒',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})